/**
 * 资质类型、证书专业以及证书级别三级联动判断
 * @param obj 证书类型
 * @param generalType 大类型(证书或资质)编码
 * @param type 联动类型  0-查询证书类型 1-根据证书类型查询证书专业  2-不加载证书级别
 * @param isLoadLevel 是否加载证书级别 0-加载 1-不加载
 * @param certificateSpecialtyObj 证书专业
 * @param certificateLevelObj 证书级别
 */
function loadCertificateType(length, obj, generalType, type, certificateSpecialtyObj, isLoadLevel, certificateLevelObj) {
    if (checkIsEmpty(type)) {
        DGG.Errors("请传入联动类型！");
        return false;
    }
    if (type === 0) {
        $(obj).find("option").remove();
        $(obj).append("<option value=''>请选择</option>");
        $.ajax({
            url: sysInfo.basePath + '/zs/certificate_type/get_certificate_type.do',
            data: {parentId:0, generalType:generalType},
            type: "POST",
            dataType: "json",
            async:false,
            success: function (result) {
                if (result.code == 0) {
                    $.each(result.data, function (i, item) {
                        $(obj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "'>" + item.certificateName + "</option>");
                    });
                } else {
                    DGG.Errors(result.msg);
                }
            }
        });
        $(obj).select2({
            placeholder: "请选择",
            allowClear: true,
            width: length,
            language: "zh-CN"
        });
    } else if (type === 1) {
        // 验证证书类型是否已经选择
        var certificateTypeId = $(obj + " option:selected").val();
        if (!checkIsEmpty(certificateTypeId)) {
            $(certificateSpecialtyObj).find("option").remove();
            $.ajax({
                url: sysInfo.basePath + '/zs/certificate_type/get_certificate_type.do',
                data: {parentId:certificateTypeId, generalType:generalType},
                type: "POST",
                dataType: "json",
                async:false,
                success: function (result) {
                    if (result.code == 0) {
                        $(certificateSpecialtyObj).append("<option value=''>请选择</option>");
                        // $(certificateSpecialtyObj).append("<option value='isAll'>全部</option>");
                        $.each(result.data, function (i, item) {
                            $(certificateSpecialtyObj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "'>" + item.certificateName + "</option>");
                        });
                    } else {
                        DGG.Errors(result.msg);
                    }
                }
            });
            $(certificateSpecialtyObj).select2({
                placeholder: "请选择",
                allowClear: false,
                width: length,
                multiple:true,
                language: "zh-CN"
            });

            if (!checkIsEmpty(isLoadLevel) && isLoadLevel === 0) {
                // 加载证书等级
                var levelStrs = $(obj + " option:selected").attr("data-level");
                $(certificateLevelObj).find("option").remove();
                $(certificateLevelObj).append("<option value=''>请选择</option>");
                var levels;
                if (!checkIsEmpty(levelStrs)) {
                    levels = levelStrs.split(",");
                }
                if (checkIsEmpty(levels)) {
                    $(certificateLevelObj).append("<option value=''>暂无等级</option>");
                } else {
                    $.ajax({
                        url: sysInfo.basePath + '/zzsc/common/translate_code.do',
                        data: {codes:levelStrs},
                        type: "POST",
                        dataType: "json",
                        async:false,
                        success: function (result) {
                            $.each(result.data, function (i, item) {
                                $(certificateLevelObj).append('<option value="' + item.code + '">' + item.name + '</option>');
                            });
                        }
                    });
                }
                $(certificateLevelObj).select2({
                    placeholder: "请选择",
                    allowClear: true,
                    width: length,
                    language: "zh-CN"
                });
            }

        } else {
            // DGG.Errors("请先选择证书类型！");
            $(certificateLevelObj).find("option").remove();
            $(certificateLevelObj).append("<option value=''>请选择</option>");
            $(certificateSpecialtyObj).find("option").remove();
            $(certificateSpecialtyObj).append("<option value=''>请选择</option>");
            return false;
        }
    }
}

/**
 * 验证证书专业长度
 * @param certificateTypeObj 证书类型obj
 * @param obj 证书专业obj
 */
function checkCertificateSpecialty(certificateTypeObj, obj) {
    var certificateTypeId = $(certificateTypeObj + " option:selected").val();
    if (!checkIsEmpty(certificateTypeId) || certificateTypeId == '') {
        var certificateSpecialtys = getVal(obj, 1);
        if (!checkIsEmpty(certificateSpecialtys) && certificateSpecialtys.length > 5) {
            DGG.Errors("最多只能选择五个证书专业！");
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

/**
 * 验证是否有查看事业部权限
 * @param obj 参考传值 $("#ddlRegType option")
 */
function checkHasSybAuth(obj) {
    var count = obj.length;
    if (checkIsEmpty(count) || count === 0) {
        return false;
    }
    return true;
}

/**
 * 加载证书类型、证书专业或者证书等级
 * @param obj          证书专业或者类型obj
 * @param objVal       证书专业或者类型的值
 * @param type       类型 0-证书类型 1-证书专业 2-证书等级 默认证书类型
 * @param generalType  大类型 资质或者证书
 * @param pid     证书类型传0  证书专业传父级id
 * @param certificateLevelObj 证书等级obj 需要加载证书等级时必传
 * @param certificateLevelVal 证书等级值 需要加载证书等级时必传
 */
function loadCertificateVal(obj, objVal, generalType, pid, type, certificateLevelObj, certificateLevelVal) {
    if (checkIsEmpty(type) || type === 0) {
        $(obj).find("option").remove();
        $.post(sysInfo.basePath + "/zs/certificate_type/get_certificate_type.do", {
            parentId: pid,
            generalType: generalType
        }, function (result) {
            if (result.code == 0) {
                var levelStrs = "";
                $.each(result.data, function (i, item) {
                    if (!checkIsEmpty(objVal) && objVal == item.id) {
                        levelStrs = item.certificateLevel;
                        $(obj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "' SELECTED>" + item.certificateName + "</option>");
                    } else {
                        $(obj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "'>" + item.certificateName + "</option>");

                    }
                });
                $(obj).select2({
                    placeholder: "请选择",
                    allowClear: true,
                    width: "100%",
                    language: "zh-CN"
                });
                // 加载证书等级
                $(certificateLevelVal).find("option").remove();
                var levels;
                if (!checkIsEmpty(levelStrs)) {
                    levels = levelStrs.split(",");
                }
                if (checkIsEmpty(levels)) {
                    $(certificateLevelVal).append("<option value=''>暂无等级</option>");
                } else {
                    $.post(sysInfo.basePath + '/zzsc/common/translate_code.do', {codes: levelStrs}, function (result) {
                        $.each(result.data, function (i, item) {
                            if (item.code === certificateLevelVal) {
                                $(certificateLevelObj).append('<option value="' + item.code + '" selected>' + item.name + '</option>');
                            } else {
                                $(certificateLevelObj).append('<option value="' + item.code + '">' + item.name + '</option>');
                            }
                        });
                    });
                }
            } else {
                DGG.Errors("系统错误，请联系管理员");
            }
        });
        $(certificateLevelObj).select2({
            placeholder: "请选择",
            allowClear: true,
            width: "100%",
            language: "zh-CN"
        });

    } else if (type == 1) {
        $(obj).find("option").remove();
        $(obj).append("<option value=''>请选择</option>");

        $.post(sysInfo.basePath + "/zs/certificate_type/get_certificate_type.do", {
            parentId: pid,
            generalType: generalType
        }, function (result) {
            if (result.code == 0) {
                $.each(result.data, function (i, item) {
                    $(obj).append("<option value='" + item.id + "' data-level='" + item.certificateLevel + "'>" + item.certificateName + "</option>");
                });
                $(obj).val(objVals).trigger('change');
            } else {
                DGG.Errors("系统错误，请联系管理员");
            }
        });
        var objVals = "";
        if (!checkIsEmpty(objVal)) {
            objVals = objVal.split(",");
        }
        $(obj).select2({
            placeholder: "请选择",
            allowClear: false,
            width: "100%",
            language: "zh-CN"
        });
    }


}